﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using System.Globalization;
using Tesla.Graphics;

namespace Tesla.Direct3D10.Graphics {
    /// <summary>
    /// Input layout key for identifying the vertex layout signature of a shader.
    /// </summary>
    internal struct InputLayoutKey : IEquatable<InputLayoutKey> {
        private int _key;
        private VertexElement[] _elements;

        public int Key {
            get {
                return _key;
            }
        }

        public VertexElement[] Elements {
            get {
                return _elements;
            }
        }

        public InputLayoutKey(VertexDeclaration decl) {
            _key = decl.GetHashCode();
            _elements = null;
        }

        public InputLayoutKey(VertexElement[] elements) {
            _elements = elements;
            int hash = VertexDeclaration.GetVertexStride(elements);
            for(int i = 0; i < elements.Length; i++) {
                hash = ((hash << 3) - hash) + elements[i].GetHashCode();
            }
            _key = hash;
        }

        public static bool operator==(InputLayoutKey a, InputLayoutKey b) {
            return a.Key == b.Key;
        }

        public static bool operator!=(InputLayoutKey a, InputLayoutKey b) {
            return a.Key != b.Key;
        }

        public override bool Equals(Object other) {
            if(other is InputLayoutKey) {
                return Equals((InputLayoutKey) other);
            }
            return false;
        }

        public bool Equals(InputLayoutKey other) {
            return this.Key == other.Key;
        }

        public override int GetHashCode() {
            return _key;
        }

        public override string ToString() {
            CultureInfo info = CultureInfo.CurrentCulture;
            return String.Format(info, "{InputLayoutKey : [{0}, {1}]}",
                new Object[] { this._key.ToString(), this._elements.ToString()});
        }
    }
}
